﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace SlidingBlocks
{
    public partial class Page : UserControl
    {
        Canvas[] cI = new Canvas[16];
        Image[] i = new Image[16];
        int[] board = new int[16];

        public Page()
        {
            InitializeComponent();
            InitBoard();
            
        }

        void InitBoard()
        {
            Uri uri = new Uri("sl.jpg", UriKind.Relative);
            int nx = 0;
            for (int ix = 0; ix < 4; ix++)
                for (int iy = 0; iy < 4; iy++)
                {
                    nx = (ix * 4) + iy;
                    i[nx] = new Image();

                    i[nx].Height = 400;
                    i[nx].Width = 400;
                    i[nx].Stretch = Stretch.UniformToFill;
                    RectangleGeometry r = new RectangleGeometry();
                    r.Rect = new Rect((ix * 100), (iy * 100), 100, 100);
                    i[nx].Clip = r;

                    i[nx].Source = new BitmapImage(uri);
                    i[nx].SetValue(Canvas.TopProperty, Convert.ToDouble( iy * 100 * -1));
                    i[nx].SetValue(Canvas.LeftProperty, Convert.ToDouble(ix * 100 * -1));

                    cI[nx] = new Canvas();
                    cI[nx].Width = 100;
                    cI[nx].Height = 100;
                    cI[nx].Children.Add(i[nx]);
                    cI[nx].SetValue(Canvas.NameProperty, "C" + nx.ToString());
                    cI[nx].MouseLeftButtonDown += 
                        new MouseButtonEventHandler(Page_MouseLeftButtonDown);
                    if (nx < 15)
                        GameContainer.Children.Add(cI[nx]);


                }

            // Mix up the pieces
            shuffle();

            // Draw the board
            drawBoard();

        }

        void shuffle()
        {
            // Initialize Board
            for (int n = 0; n < 15; n++)
            {
                board[n] = n;
            }
            Random rand = new Random(System.DateTime.Now.Second);
            for (int n = 0; n < 100; n++)
            {
                int n1 = rand.Next(15);
                int n2 = rand.Next(15);
                if (n1 != n2)
                {
                    int tmp = board[n1];
                    board[n1] = board[n2];
                    board[n2] = tmp;
                }
            }
            board[15] = -1;

        }

        void drawBoard()
        {
            int nx = 0;
            int ny = 0;
            for (int n = 0; n < 15; n++)
            {
                nx = n / 4;
                ny = n % 4;
                if (board[n] >= 0)
                {
                    cI[board[n]].SetValue(Canvas.TopProperty, Convert.ToDouble(ny * 100));
                    cI[board[n]].SetValue(Canvas.LeftProperty, Convert.ToDouble(nx * 100));

                }
            }
        }

        void checkWinner()
        {
            bool bCompleted = true;
            for (int n = 0; n < 15; n++)
            {
                if (n != board[n])
                {
                    bCompleted = false;
                    break;
                }
            }
            if (bCompleted)
            {
                // The Player has won the game....do something nice for them.
            }
        }

        void Page_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Canvas c = sender as Canvas;
            int nCanvasID = -1;
            int nBoardLoc = -1;
            int nEmptyLoc = -1;
            for (int i = 0; i < 16; i++)
            {
                if (c == cI[i])
                {
                    nCanvasID = i;
                    break;
                }
            }
            for (int i = 0; i < 16; i++)
            {
                if (board[i] == nCanvasID)
                {
                    nBoardLoc = i;
                }
                else if (board[i] == -1)
                {
                    nEmptyLoc = i;
                }
            }

            // Check if we can move
            if ((nBoardLoc == nEmptyLoc + 1) || 
                (nBoardLoc == nEmptyLoc - 1) || 
                (nBoardLoc == nEmptyLoc + 4) || 
                (nBoardLoc == nEmptyLoc - 4))
            {
                int nx = nEmptyLoc/4;
                int ny = nEmptyLoc%4;

                cI[nCanvasID].SetValue(Canvas.TopProperty, Convert.ToDouble(ny * 100));
                cI[nCanvasID].SetValue(Canvas.LeftProperty, Convert.ToDouble(nx * 100));

                board[nEmptyLoc] = nCanvasID;
                board[nBoardLoc] = -1;

                checkWinner();
            }
            else
            {
                // do nothing
            }

           
        

        }

    }
}
